class Photo < ActiveRecord::Base
  
  belongs_to :user
  has_and_belongs_to_many :albums
  has_and_belongs_to_many :tags
  acts_as_list

    # Dimensions of image sizes
    LARGE_HEIGHT = 600
    LARGE_WIDTH = 600
    MEDIUM_HEIGHT = 300
    MEDIUM_WIDTH = 225
    THUMB_HEIGHT = 100
    THUMB_WIDTH = 100

    UPLOAD_FOLDER = RAILS_ROOT + '/public/slave_photos/'
    UPLOAD_FOLDER_OR = RAILS_ROOT + '/public/photos/'
    WEB_FOLDER = '/slave_photos/'
    WEB_FOLDER_OR = '/photos/'
    
    def to_url(size = :medium)
      return WEB_FOLDER + "#{id}_#{size.to_s}.jpg" if [:large, :medium, :thumbnail].include?(size)
      raise 'Bad size'
    end
    
    def to_url_or(k)
      size = :original
      return WEB_FOLDER_OR + k + "/" + "#{id}.jpg" if [:original].include?(size)
      raise 'Bad size'
    end
  
    
    #There are methods to work with tags below
    def get_tags
      self.tags
    end
    
    def add_tag(tagname)
    tagname = tagname.strip
      tag=Tag.find_by_title(tagname)
      if tag==nil 
        tag=Tag.create(:title => tagname)
      end
      if !(self.tags.include?(tag))
        self.tags<<tag
      end
    end
    
    def delete_tag(tagname)
    tagname = tagname.strip
      #Not debbuged yet
      tag=Tag.find_by_title(tagname)
      if tag
        tag.photos.delete(self)
        if tag.photos.empty? 
          tag.destroy
        end
      end
    end
    
    #There are methods to work with tags above

    # write takes a file from a form and writes all our sizes to disk
  def write(file, title, i)
    raise "No file uploaded" if file.nil?
    #file = file.read
      
    [:original, :large, :medium, :thumbnail].each do |size|
      logger.info "Generating #{size} image. file type: #{file.class}"
        
      if (size == :original)
        if (i == 1)
          resize(size, file).write(UPLOAD_FOLDER_OR + title + '/' +  "#{id}.jpg") 
        end 
      else
        resize(size, file).write(UPLOAD_FOLDER + "#{id}_#{size}.jpg")
      end
      logger.info "#{size.to_s} image written to disk: " + UPLOAD_FOLDER + "#{id}_#{size}.jpg"
    end
  end
    
#  def write_img(title, image)
#    raise "No file uploaded" if image.nil?
#         
#    [:original, :large, :medium, :thumbnail].each do |size|
#      logger.info "Generating #{size}"
#        
#      if (size == :original)
#        image.write(UPLOAD_FOLDER_OR + title + '/' +  "#{id}.jpg") 
#      else
#        resize_img(size, image).write(UPLOAD_FOLDER + "#{id}_#{size}.jpg")
#     end
#      logger.info "#{size.to_s} image written to disk: " + UPLOAD_FOLDER + "#{id}_#{size}.jpg"
#    end
#  end

  def rm(title)
    logger.info "Removing #{id}_*.jpg from disk."
    FileUtils.rm Dir.glob(UPLOAD_FOLDER + "#{id}_*.jpg")
    if (title != '')
      FileUtils.rm Dir.glob(UPLOAD_FOLDER_OR + title +'/' + "#{id}.jpg")
    end
  end
    
   private

    # resize takes a size and a blob image and sizes it accordingly
    def resize(size = :medium, file = nil)
      #image = Magick::Image.from_blob(file).first
      image = Magick::Image.read(file).first
      case size
      when :original
        return image
      when :large
        geometry = "#{LARGE_WIDTH}x#{LARGE_HEIGHT}"
      when :medium
        geometry = "#{MEDIUM_WIDTH}x#{MEDIUM_HEIGHT}"
      when :thumbnail
        geometry = "#{THUMB_WIDTH}x#{THUMB_HEIGHT}"
        return crop_part(image, geometry)
      else
        raise 'No size recognized'
      end
      image.change_geometry!(geometry) do |cols,rows,img|
        img.resize!(cols,rows)
      end
    end

#  def resize_img(size = :medium, image = nil)
#    case size
#      when :original
#        return image
#      when :large
#        geometry = "#{LARGE_WIDTH}x#{LARGE_HEIGHT}"
#      when :medium
#        geometry = "#{MEDIUM_WIDTH}x#{MEDIUM_HEIGHT}"
#      when :thumbnail
#        geometry = "#{THUMB_WIDTH}x#{THUMB_HEIGHT}"
#        return crop_part(image, geometry)
#      else
#        raise 'No size recognized'
#      end
#    image.change_geometry!(geometry) do |cols,rows,img|
#    img.resize!(cols,rows)
#    end
# end

 

    def crop_part(image, size)
      target_w = size.split(/x/)[0].to_i
      target_h = size.split(/x/)[1].to_i
      orig_w = image.columns
      orig_h = image.rows

      if (orig_w.to_f/orig_h) > (target_w.to_f/target_h)
        image.change_geometry!("x"+target_h.to_s) { |cols, rows, img| img.resize!(cols, rows) }
        image.crop!((image.columns - target_w) / 2, 0, target_w, target_h)
      else
        image.change_geometry!(target_w.to_s+"x") { |cols, rows, img| img.resize!(cols, rows) }
        image.crop!(0, (image.rows - target_h) / 2, target_w, target_h)
      end
    end

    def before_destroy
      rm
    end


end